var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var session = require('express-session');
var router = require('./router.js');
var ticketinfo = require('./routes/ticketinfo');
var comment = require('./routes/comment');

var app = express();

app.use('/public', express.static(path.join(__dirname,'./public/')));
app.use('/node_modules', express.static(path.join(__dirname, './node_modules/')));

app.engine('html', require('express-art-template'));
app.set('views', path.join(__dirname, './views/'));

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

//express-session来使用session和cookie
//cnpm i express-session
//配置插件后 就可以通过req.session来访问和设置session成员
app.use(session({
    secret:'keyboard cat',
    resave:false,
    saveUninitialized:true
}));

//接受http与https请求
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    next();
});
//把路由挂在到app中
app.use(router);
app.use(ticketinfo);
app.use(comment);

app.listen(3000, function () {
    console.log('running...')
});
